from PyQt4 import QtGui
from PyQt4.QtCore import Qt

class TartBarLamp(QtGui.QWidget):
        def __init__(self, *args, **kw):
		QtGui.QWidget.__init__(self, *args, **kw)
		self.setGeometry(0, 0, 39, 41)
#
#		this widget is going to be our viewport
#		we set it to be smaller than the grad
#		so the grad will be clipped to size
#
		vport = QtGui.QLabel(self)
		vport.setGeometry(8, 8, 25, 25)
#
#		now here's the grad itself.
#		I've used one about 10x the size of the port
#		but any size will do. bigger the grad, the
#		smoother the transition should be
#
		grad = QtGui.QLabel(vport)
		grad.setGeometry(0,0,34,229)
		path = "resources/grad.png"
		grad.setPixmap(QtGui.QPixmap(path))
		self.grad = grad
#
#		I want a grey lamp for when there is no data
#		so this is a plain grey texture. 
#
#		we position it over the viewport and hide it
#		so the lamp is initially ON
#
		grey = QtGui.QLabel(self)
		grey.setGeometry(7, 7, 28, 31)
		path = "resources/tart_bar_lamp_grey.png"
		grey.setPixmap(QtGui.QPixmap(path))
		grey.hide()
		self.grey = grey
#
#		this is the lamp image that sits over the
#		viewport
#
		lamp = QtGui.QLabel(self) 
		lamp.setGeometry(0, 0, 39, 41)
		path = "resources/tart_bar_lamp_2.png"
		lamp.setPixmap(QtGui.QPixmap(path))

#
#	set the lamp to represent X% value for a stat
#
#	expects an integer percentage
#
	def set_percent(self, percent):
#
#		first make sure it's turned ON
#
		self.switch(True)
#
#		convert to floating point
#
		fval = percent / 100.0
#
#		the way I set the grad up, green is at the
#		top of the strip. But I want green to mean
#		100% so I need to invert the ratio.
#
#		I'm sure there's a simpler way to go about this bit
#
		fval = 1.0 - fval
#
#		now scroll the grad strip down behind the viewport
#		proportionally speaking
#
		offset = int((229-25) * fval * -1)
		self.grad.move(0, offset)

#
#	toggle switch on and off
#
	def switch(self, bval):
		if bval:
			self.grey.hide()
		else:
			self.grey.show()

if __name__ == "__main__":
	import sys

	app = QtGui.QApplication(sys.argv)

	top = QtGui.QLabel()
	top.setGeometry(0, 0, 520, 80)
	path = "resources/wood.jpg"
	top.setPixmap(QtGui.QPixmap(path))

	xoff = 20
	lamp1 = TartBarLamp(top)
	lamp1.move(xoff,20)
	lamp1.set_percent(100)

	xoff += 70
	lamp2 = TartBarLamp(top)
	lamp2.move(xoff,20)
	lamp2.set_percent(80)

	xoff += 70
	lamp3 = TartBarLamp(top)
	lamp3.move(xoff,20)
	lamp3.set_percent(60)

	xoff += 70
	lamp4 = TartBarLamp(top)
	lamp4.move(xoff,20)
	lamp4.set_percent(40)

	xoff += 70
	lamp5 = TartBarLamp(top)
	lamp5.move(xoff,20)
	lamp5.set_percent(20)

	xoff += 70
	lamp6 = TartBarLamp(top)
	lamp6.move(xoff,20)
	lamp6.set_percent(0)

	xoff += 70
	lamp7 = TartBarLamp(top)
	lamp7.move(xoff,20)
	lamp7.switch(False)
	lamp6.set_percent(0)

	top.show()
	sys.exit(app.exec_())


